package teach.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import teach.aistar.model.entity.WebSeckill;
import teach.aistar.model.vo.Result;
import teach.aistar.service.ISeckillService;
import teach.aistar.service.IWebSeckillService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 本类用来演示: 秒杀控制器
 *
 * @author: success
 * @date: 2021/5/13 11:00 上午
 */
//@RestController
    @Controller

public class SecKillController {

    @Autowired
    private ISeckillService seckillService;

    @Autowired IWebSeckillService iWebSeckillService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/sec")
    @ResponseBody
    public Result secKill(HttpServletRequest request, HttpSession session){
//        Map<String,Object> result = new HashMap<>();
        String Id = request.getParameter("id");
        Long id = Long.valueOf(Id);

        System.out.println(Id);
        Long secId = Long.valueOf(Id);
        String uName = request.getParameter("uname");
        System.out.println("uName....Controller"+uName);


        WebSeckill id1 = iWebSeckillService.findId(id);
        Double seckillPrice = id1.getSeckillPrice();


//        String prices = request.getParameter("prices");
//        System.out.println("prices....."+prices);
//        Double pricez = Double.valueOf(prices);
        session.setAttribute("prices",seckillPrice);

        //1. 获取秒杀商品的状态
        String status = seckillService.findStatus(secId);

        //判断状态
        //不适合秒杀
        if("0".equals(status)){
          return new Result("0","大人还没开始呢，耐心等待哦",null);
        }

        if("2".equals(status)){
            return new Result("2","又睡过了吧，秒杀都结束了哦",null);
        }

        //适合秒杀
        //redis秒杀商品存储形式
        //seckill_product_2 => [2,2,2,2,2]
        //从redis中直接删除1个
        //redis是一个单线程的
        //删除并且返回这个对象
        ListOperations list = redisTemplate.opsForList();
        Object o = list.leftPop("seckill_product_"+secId);
        //判断o - 是否为null,如果为null,说明商品已经被秒杀完
        //售罄了
        if(o == null){
            return new Result("3","啊啊啊。好遗憾好可惜，秒杀商品已经卖光了，下次记得蹲点哦",null);
        }

        //可以正常秒杀
        //注意的问题 - 程序代码能够运行到此处
        //无论这个人是第几次请求过来
        //它都已经从list中删除了1个商品...

        //解决问题 - 1个人只能拥有1个秒杀名额
        SetOperations set = redisTemplate.opsForSet();
        boolean flag = set.isMember("user_product_"+secId,uName);
        if(flag){

            list.leftPush("seckill_product_" + secId, secId);
            return new Result("4","您已经抢购成功了，大人把机会留给手速慢的小伙伴吧",null);
        }

        //成功秒杀
        //操作redis - 存放秒杀成功的信息
        //key=> user_product_2 => ["admin","tom","jack"]

        set.add("user_product_"+secId,uName);

        //把秒杀的核心信息发送到rabbitMQ中s

        String email = (String) session.getAttribute("email");

        if(email!=null) {
            seckillService.save2Rbm(uName, secId,email,seckillPrice);
        }

        if(email==null)
        {
            seckillService.saverRbm(uName,secId,seckillPrice);
        }

       return new Result("200","恭喜大大抢购成功",null);
    }

}
